package com.njtoyo.taxi.admin.service.data.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.njtoyo.taxi.admin.handler.FilterHandler;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.logic.data.DashboardDriverServiceDailyLogic;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardDriverServiceDaily;
import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.WorkingTaxiChartPresenter;
import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.WorkingTaxiCountChartPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.data.chart.ChartQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.data.data_analysis.ChartWrapper;
import com.njtoyo.taxi.admin.service.data.WorkingTaxiService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@DS(Const.SECONDARY_DS)
public class WorkingTaxiServiceImpl implements WorkingTaxiService {

    @Autowired
    private DashboardDriverServiceDailyLogic dashboardDriverServiceDailyLogic;

    @Override
    public RestResult<List<WorkingTaxiChartPresenter>> workingTaxiChart(AdminUser adminUser, ChartWrapper wrapper) {
        FilterHandler filterHandler = new FilterHandler(adminUser, wrapper.getCantonId(), wrapper.getCompanyId());

        List<DashboardDriverServiceDaily> result = dashboardDriverServiceDailyLogic.getRateChart(filterHandler, wrapper);

        List<WorkingTaxiChartPresenter> res = new ArrayList<>();

        Double taxiRate = filterHandler.getFakeMultiple().getWorkingTaxiRate();
        result.forEach(i -> {
            WorkingTaxiChartPresenter item = new WorkingTaxiChartPresenter();
            item.setStatsDate(i.getStatsDate());
            // 车辆总数
            item.setTotal(i.getTaxiTotal());

            // 容错
            if (0 != i.getTaxiTotal()) {
                BigDecimal rate = NumberUtil.div(i.getTaxiServiceTotal(), i.getTaxiTotal());
                BigDecimal rateFormat = NumberUtil.round(rate, 4);

                // 翻倍率
                if (ObjectUtil.isNotEmpty(taxiRate)) {
                    // 出车率
                    BigDecimal multiplyRate = rateFormat.multiply(BigDecimal.valueOf(taxiRate));
                    item.setValue(multiplyRate.compareTo(BigDecimal.ONE) >= 0 ? BigDecimal.ONE : multiplyRate);
                } else {
                    // 出车率
                    item.setValue(rateFormat);
                }
            } else {
                item.setValue(BigDecimal.ZERO);
            }

            // 出车数
            item.setWorking(i.getTaxiServiceTotal());

            res.add(item);
        });
        return RestResult.success(res);
    }

    @Override
    public RestResult<List<WorkingTaxiCountChartPresenter>> chart(AdminUser adminUser, ChartQueryWrapper wrapper) {
        return null;
    }
}
